import { Tree } from 'antd'
import { memo } from 'react'
interface TreeProps {
    disabled?: boolean
    value?: any
    onChange?: (value: any) => void
    checkable?: boolean
    treeData?: any[]
}

function DefaultTree(props: TreeProps) {
    function onCheck(selectedKeys: any, info: any) {
        const { halfCheckedKeys } = info;
        selectedKeys = selectedKeys.concat(halfCheckedKeys);
        console.log(selectedKeys)
        props.onChange && props.onChange(selectedKeys)
    }
    let val: any = []
    if (props.value) {
        val = [...props.value]
        for (let i in props.treeData) {
            let index = val.indexOf(props.treeData[parseInt(i)].id)
            if (index != -1) {
                val.splice(index, 1)
            }
        }
    }
    console.log(val)
    return (
        <Tree
            checkedKeys={val}
            autoExpandParent={true}
            onCheck={onCheck}
            fieldNames={
                { title: "name", key: "id" }
            }
            disabled={props.disabled}
            treeData={props.treeData}
            checkable >
        </Tree>
    )
}


export default memo(DefaultTree)

